//
//  APPSelectListAlertViewControl.swift
//  APP
//
//  Created by LXF on 2018/1/8.
//  Copyright © 2018年 Rdxer. All rights reserved.
//

import Foundation
import XXAutoLayout


protocol APHSelectListAlertViewItemModel:NSObjectProtocol {
    var alert_isSelected: Bool? { get set}
    var alert_title:String? {get}
}

/**
 let alert = FlowSelectStructureTableViewAlertViewController.init(dataList: self.approvalStructureList!)
 
 alert.dataList.first?.isSelected = true
 
 alert.title_Label.text = "选择组织"
 
 alert.clickConfirmButtonCallBack = { [weak self,weak alert] (vc) in

     guard self != nil else{
     return
     }
 
     self?.approvalStructure = alert?.dataList.filter({$0.isSelected}).first
     HUD.show()
     self?.doFlow6()
 }
 
 self.alertView?.dismiss(animated: false, completion: nil)
 self.alertView = alert.show(at: self)
 */
class APHSelectListAlertViewController: APHCustomViewAlertViewController {
    
    var selectList:[APHSelectListAlertViewItemModel]{
        return dataList.filter({$0.alert_isSelected ?? false})
    }
    
    var dataList:[APHSelectListAlertViewItemModel]
    var tableView: UITableView
    
    
    var configCell:((_ cell:APHSelectListAlertCell,_ model:APHSelectListAlertViewItemModel)->())? = nil
    
    
    init(dataList: [APHSelectListAlertViewItemModel]) {
        self.dataList = dataList
        
        self.tableView = UITableView.init(frame: .zero, style: .plain)
        super.init(customView: self.tableView)
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(APHSelectListAlertCell.self, forCellReuseIdentifier: "cell")
        self.tableView.tableFooterView = UIView.init()
        tableView.separatorInset = .init(top: 0, left: 0, bottom: 0, right: 0)
    }
}

extension APHSelectListAlertViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! APHSelectListAlertCell
        
        cell.model = dataList[indexPath.row]
        
        configCell?(cell,dataList[indexPath.row])
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dataList.forEach({ a in a.alert_isSelected = false})
        dataList[indexPath.row].alert_isSelected = true
        tableView.reloadData()
    }
}


class APHSelectListAlertCell: XXBaseTableViewCell<APHSelectListAlertViewItemModel> {
    
    override func modelDidSet(oldValue: APHSelectListAlertViewItemModel?, newValue: APHSelectListAlertViewItemModel?) {
        title_Label.text = newValue?.alert_title
        select_Button.isSelected = newValue?.alert_isSelected ?? false
    }
    
    override func prepareSubViews() {
        contentView.addSubviews([
            title_Label,
            select_Button
        ])
        select_Button.xx_AlignInner(with: .centerRight, referView: contentView, size: CGSizeNull(), offset: .init(x: -7, y: 0))
        select_Button.snp.makeConstraints { (make) in
            make.width.equalTo(18)
        }
        title_Label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(7)
            make.right.equalTo(select_Button.snp.left).offset(-7)
            make.centerY.equalToSuperview()
        }
    }
    
    var title_Label = UILabel.init().then {
        $0.textColor = AppColor.textColor
        $0.font = 14.toFont
        $0.lineBreakMode = .byTruncatingHead
    }
    
    open var select_Button = UIButton.init().then {
        $0.setImage(( #imageLiteral(resourceName: "check_unchecked")), for: .normal)
        $0.setImage(( #imageLiteral(resourceName: "check_checked")), for: .selected)
        $0.isUserInteractionEnabled = false
    }
}
